{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reranking with RAGatouille\n",
    "\n",
    "In this quick example, we'll use the `RAGPretrainedModel` magic class to demonstrate how to **re-rank documents** retrieved by another retriever, such as **your existing RAG pipeline**.\n",
    "\n",
    "First, as usual, let's load up a pre-trained ColBERT model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/bclavie/miniforge3/envs/ragatouille/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Jan 25, 18:45:59] Loading segmented_maxsim_cpp extension (set COLBERT_LOAD_TORCH_EXTENSION_VERBOSE=True for more info)...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/bclavie/miniforge3/envs/ragatouille/lib/python3.11/site-packages/torch/cuda/amp/grad_scaler.py:125: UserWarning: torch.cuda.amp.GradScaler is enabled, but CUDA is not available.  Disabling.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from ragatouille import RAGPretrainedModel\n",
    "\n",
    "RAG = RAGPretrainedModel.from_pretrained(\"colbert-ir/colbertv2.0\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that our model is loaded, we must build an index of our documents for our first retrieval step! In the real world, you'd likely already have some sort of pipeline doing this, which we're going to emulate here, using bge embeddings Spotify's excellent `voyager` library."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sentence_transformers import SentenceTransformer\n",
    "from voyager import Index, Space\n",
    "\n",
    "class MyExistingRetrievalPipeline:\n",
    "    index: Index\n",
    "    embedder: SentenceTransformer\n",
    "\n",
    "    def __init__(self, embedder_name: str = \"BAAI/bge-small-en-v1.5\"):\n",
    "        self.embedder = SentenceTransformer(embedder_name)\n",
    "        self.collection_map = {}\n",
    "        self.index = Index(\n",
    "            Space.Cosine,\n",
    "            num_dimensions=self.embedder.get_sentence_embedding_dimension(),\n",
    "        )\n",
    "\n",
    "    def index_documents(self, documents: list[str]) -> None:\n",
    "        # There's very few documents in our example, so we don't bother with batching\n",
    "        for document in documents:\n",
    "            self.collection_map[self.index.add_item(self.embedder.encode(document['content']))] = document['content']\n",
    "\n",
    "    def query(self, query: str, k: int = 10) -> list[str]:\n",
    "        query_embedding = self.embedder.encode(query)\n",
    "        to_return = []\n",
    "        for idx in self.index.query(query_embedding, k=k)[0]:\n",
    "            to_return.append(self.collection_map[idx])\n",
    "        return to_return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "existing_pipeline = MyExistingRetrievalPipeline()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that our mock of existing pipeline is set up, let's index some documents with it! We'll re-use our favourite combo from the previous examples: `CorpusProcessor` and `get_wikipedia_page()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ragatouille.utils import get_wikipedia_page\n",
    "from ragatouille.data import CorpusProcessor\n",
    "\n",
    "corpus_processor = CorpusProcessor()\n",
    "\n",
    "documents = [get_wikipedia_page(\"Hayao Miyazaki\"), get_wikipedia_page(\"Studio Ghibli\"), get_wikipedia_page(\"Princess Mononoke\"), get_wikipedia_page(\"Shrek\")]\n",
    "documents = corpus_processor.process_corpus(documents, chunk_size=200)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, let's add those to the voyager index so we can simulate a real query:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "existing_pipeline.index_documents(documents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"Another defining feature is Hisaishi's unique use of leitmotif, rather than a singular song being associated with one character, the motif is the theme of the film. Hisaishi began using leitmotif in Ghibli films first in Howl's Moving Castle.\\n\\n\\n== Criticism ==\\nRayna Denison argues that, despite the feminist themes of Ghibli films, the studio has been reluctant to promote women within the company and regularly overworks its laborers, including on public holidays.Nathalie Pascaru and Maxim Tvorun-Dunn criticize the studio for what they see as the increasing commercialization of their films' iconography through plastic merchandise and tourist destinations, undermining the environmentalist themes of their films through industrial actions which are detrimental to the environment; as well as for turning their films' characters into decontextualized pop-culture references rather than multidimensional characters used to convey stories.\",\n",
       " 'In September 2023, Nippon TV announced that Studio Ghibli would become a unit of Nippon Television Holdings in October.\\n\\n\\n== Name ==\\nThe name \"Ghibli\" was chosen by Miyazaki from the Italian noun ghibli (also used in English), an Italianization of the Libyan Arabic name for a hot desert wind (قبلي ghiblī) and nickname of the Italian Caproni Ca.309 aircraft. The name was chosen by Miyazaki due to his passion for aircraft and also for the idea that the studio would \"blow a new wind through the anime industry\". Although the Italian word would be more accurately transliterated as \"Giburi\" (ギブリ), with a hard g sound, the studio\\'s name is written in Japanese as Jiburi (ジブリ, [d͡ʑiꜜbɯ̟ᵝɾʲi] ).',\n",
       " \"ISBN 9784866580227.Documentaries\\n\\nThis Is How Ghibli Was Born (ジブリはこうして生まれた, Jiburi wa kōshite umareta). 1998 documentary, Nippon TV, 28 min.\\nThe Kingdom of Dreams and Madness (夢と狂気の王国, Yume to Kyoki no Okoku). 2013 documentary by Mami Sunada, 118 min.\\nNever-Ending Man: Hayao Miyazaki (終わらない人 宮﨑駿, Owaranai Hito Miyazaki Hayao). 2016 documentary by Kaku Arakawa, 70 min.\\n\\n\\n== External links ==\\n\\nOfficial website (in Japanese)\\nStudio Ghibli  at Anime News Network's encyclopedia\",\n",
       " \"== History ==\\n\\n\\n=== Tokuma Shoten era ===\\n\\nFounded on June 15, 1985, Studio Ghibli was headed by directors Hayao Miyazaki and Isao Takahata and producer Toshio Suzuki. Miyazaki and Takahata had already had long careers in Japanese film and television animation and had worked together on The Great Adventure of Horus, Prince of the Sun in 1968 and the Panda! Go, Panda! films in 1972 and 1973. Suzuki had been an editor at Tokuma Shoten's Animage manga magazine.The studio was founded after the success of the 1984 film Nausicaä of the Valley of the Wind.\",\n",
       " \"It is the first stage adaptation of a Studio Ghibli work. The contact between Whole Hog Theatre and Studio Ghibli was facilitated by Nick Park of Aardman Animations after he sent footage of Whole Hog performances to Studio Ghibli's Toshio Suzuki. The play features large puppets made out of recycled and reclaimed materials.The first performances were scheduled for London's New Diorama Theatre and sold out in 72 hours, a year in advance. In March 2013, it was announced that the show would transfer to Japan after its first run of shows in London. A second series of performances followed in London after the return from Tokyo. The second run of London performances sold out in four and half hours. The play received positive reviews and was one of Lyn Gardner's theater picks in The Guardian.\\n\\n\\n== References ==\\n\\n\\n=== Citations ===\\n\\n\\n=== Book and journal sources ===\\n\\n\\n=== Online and other sources ===\",\n",
       " 'For the purposes of the list below, films that appear in Studio Ghibli\\'s official filmography are listed. Other Studio Ghibli productions are listed here.\\n\\n\\n=== Feature films ===\\n\\n\\n=== Television ===\\n\\n\\n== Style and themes ==\\n\\nStudio Ghibli films are mostly hand-drawn using rich watercolor and acrylic paints. The films use traditional methods of making animation where every frame is drawn and colored by hand. Computer animation techniques are used sparingly. All the Studio Ghibli films use bright colors, and have a \"whimsical and joyful aesthetic\". Studio Ghibli’s art style tends to be more of a cozy European style that put a lot of undertones on the background and nature in the scene.The films often focus on the lives of youth, especially school children. Common themes include the risks posed by progress to tradition, environmentalism and the natural world, independent female protagonists, the cost of war, and youth.',\n",
       " \"Madman Entertainment released the film in Australia and New Zealand.\\n\\n\\n== Works ==\\n\\nWhile not technically Studio Ghibli films, The Great Adventure of Horus, Prince of the Sun (1968), Panda! Go Panda! (1972), The Castle of Cagliostro (1979), Jarinko Chie (1981), Gauche the Cellist (1982), Nausicaä of the Valley of the Wind (1984), The Red Turtle (2016), Mary and the Witch's Flower (2017) and Modest Heroes (2018) are sometimes grouped together with the Studio Ghibli library (particularly with the Ghibli ga Ippai home video collection released by Walt Disney Studios Japan) due to their ties to the studio.\",\n",
       " \"Studio Ghibli, Inc. (Japanese: 株式会社スタジオジブリ, Hepburn: Kabushiki-gaisha Sutajio Jiburi) is a Japanese animation studio based in Koganei, Tokyo. It has a strong presence in the animation industry and has expanded its portfolio to include various media formats, such as short subjects, television commercials, and two television films. Their work has been well-received by audiences and recognized with numerous awards. Their mascot and most recognizable symbol, the character Totoro from the 1988 film My Neighbor Totoro, is a giant spirit inspired by raccoon dogs (tanuki) and cats (neko). Among the studio's highest-grossing films are Spirited Away (2001), Howl's Moving Castle (2004), and Ponyo (2008).\",\n",
       " 'The 2016 animated fantasy film The Red Turtle, directed and co-written by Dutch-British animator Michaël Dudok de Wit in his feature film debut, was a co-production between Studio Ghibli and Wild Bunch.In February 2017, Toshio Suzuki announced that Hayao Miyazaki had come out of retirement to direct a new feature film with Studio Ghibli.On November 28, 2017, Koji Hoshino stepped down as president; he was replaced by Kiyofumi Nakajima (former Ghibli Museum director). Hoshino was then appointed as Chairman of Studio Ghibli.In May 2020, Toshio Suzuki confirmed that a new film from Gorō Miyazaki is in development at Studio Ghibli. On June 3, 2020, Studio Ghibli announced that the film would be an adaptation of the novel Earwig and the Witch by Diana Wynne Jones.',\n",
       " \"On June 3, 2020, Studio Ghibli announced that the film would be an adaptation of the novel Earwig and the Witch by Diana Wynne Jones. The film was announced as the first full 3D CG animated Ghibli film and slated for a television premiere on NHK in late 2020. The company had a net income of ¥1.253 billion, and a total asset worth ¥24.521 billion by August 2021.On November 1, 2022, the Studio Ghibli themed amusement park Ghibli Park opened.On April 4, 2023, Koji Hoshino announced that he had stepped down as chairman, and would serve as a representative director before planning to exit Studio Ghibli completely during the company's annual general shareholder's meeting in June, one month prior to the release of director Hayao Miyazaki's final movie The Boy and the Heron on July 14.\",\n",
       " \"== Distribution rights ==\\n\\n\\n=== Theatrical and home media rights ===\\n\\n\\n==== Japan ====\\nIn Japan, the company's films (along with The Castle of Cagliostro and all other Lupin the Third titles for movie theaters as well as Mary and the Witch's Flower) are distributed by Toho theatrically, except for Castle in the Sky, Kiki's Delivery Service (which were distributed by Toei Company along with Nausicaä of the Valley of the Wind, with Toei producing The Great Adventure of Horus, Prince of the Sun), and My Neighbors the Yamadas, which was distributed by Shochiku.\\nFor home media, a majority of Studio Ghibli releases are distributed by Walt Disney Studios Japan. This also includes Nausicaä of the Valley of the Wind, The Castle of Cagliostro and Mary and the Witch's Flower.\",\n",
       " 'The studio is also known for its strict \"no-edits\" policy in licensing their films abroad due to Nausicaä of the Valley of the Wind being heavily edited for the film\\'s release in the United States as Warriors of the Wind.\\n\\n\\n=== Independent era ===\\nBetween 1999 and 2005, Studio Ghibli was a subsidiary brand of Tokuma Shoten; however, that partnership ended in April 2005, when Studio Ghibli was spun off from Tokuma Shoten and was re-established as an independent company with relocated headquarters.\\nOn February 1, 2008, Toshio Suzuki stepped down from the position of Studio Ghibli president, which he had held since 2005, and Koji Hoshino (former president of Walt Disney Japan) took over. Suzuki said he wanted to improve films with his own hands as a producer, rather than demanding this from his employees.',\n",
       " 'Atsushi Takahashi (Spirited Away)\\nTakashi Watanabe (Nausicaä of the Valley of the Wind, Slayers, Boogiepop Phantom and Shakugan no Shana)\\n\\n\\n== See also ==\\nGhibli Museum in Mitaka, Tokyo\\nGhibli Park in Nagakute, Aichi\\nStudio Kajino, a subsidiary of Studio Ghibli\\nYasuo Ōtsuka\\nStudio Ponoc, founded by former members of Studio Ghibli\\nList of Japanese animation studios\\n\\n\\n== Notes ==\\n\\n\\n== References ==\\n\\n\\n== Further reading ==\\nAlpert, Steve. Sharing a House with the Never-Ending Man: 15 Years at Studio Ghibli. Berkeley: Stone Bridge Press, 2020. ISBN 978-1611720570.\\nCavallaro, Dani. The Animé Art of Hayao Miyazaki.',\n",
       " 'Miyazakiworld: A Life in Art. New Haven, CT: Yalue University Press, 2019. ISBN 978-0300248593.\\nOdell, Colin, and Michelle Le Blanc. Studio Ghibli: The Films of Hayao Miyazaki and Isao Takahata. Harpenden, Hertfordshire, England: Kamera, 2009. ISBN 978-1-84243-279-2. OCLC 299246656.\\nSuzuki, Toshio. Mixing Work with Pleasure: My Life at Studio Ghibli. Tokyo: Japan Publishing Industry Foundation for Culture, 2018. ISBN 9784866580227.Documentaries\\n\\nThis Is How Ghibli Was Born (ジブリはこうして生まれた, Jiburi wa kōshite umareta).',\n",
       " \"He was named a Person of Cultural Merit by the Japanese government in November 2012, for outstanding cultural contributions. His other accolades include eight Tokyo Anime Awards, eight Kinema Junpo Awards, six Japan Academy Awards, five Annie Awards, and three awards from the Anime Grand Prix and the Venice Film Festival.\\n\\n\\n== Notes ==\\n\\n\\n== References ==\\n\\n\\n== Sources ==\\n\\n\\n== External links ==\\n\\nStudio Ghibli (in Japanese)\\nHayao Miyazaki  at Anime News Network's encyclopedia\\nHayao Miyazaki at IMDb\\nHayao Miyazaki at Library of Congress, with 14 library catalogue records\",\n",
       " \"It was a critical and commercial blockbuster, becoming the highest-grossing film in Japan of 1997, and also held Japan's box office record for domestic films until 2001's Spirited Away, another Miyazaki film. It was dubbed into English with a script by Neil Gaiman and initially distributed in North America by Miramax, where it sold well on home media despite not performing strongly at the box office. The film greatly increased Ghibli's popularity and influence outside Japan.\\n\\n\\n== Plot ==\\nIn Muromachi Japan, an Emishi village is attacked by a hideous demon. The last Emishi prince, Ashitaka, kills it before it reaches the village, but it grasps his arm and curses him before its death. The curse grants him superhuman strength, but it also causes him pain and will eventually kill him.\",\n",
       " 'This also includes Nausicaä of the Valley of the Wind, The Castle of Cagliostro and Mary and the Witch\\'s Flower. Pony Canyon occasionally releases Ghibli documentaries on home media, and also distributes rental versions of Ghibli\\'s movies under a deal with Disney. Pony Canyon also fully distributed the standalone version of Earwig and the Witch on home media.\\nBefore the Disney deal, Tokuma Shoten released Ghibli movies themselves through their \"Animage Video\" imprint, as well as all Laserdisc releases of the movies, as the Disney deal did not include that format.\\n\\n\\n==== International ====\\nAfter purchasing the global distribution rights from World Film Corporation, Manson International and Showmen, Inc.',\n",
       " \"Factory.Outside Asia (including Japan) and North America since 2003, Goodfellas (a former subsidiary of Wild Bunch, formerly known as Wild Bunch International) has been Studio Ghibli's international sales holder.\\nIndividual rights to Ghibli's films are held by various third parties, including Wild Bunch (France and Belgium), Elysian Film Group (United Kingdom and Ireland), Leonine (Germany), Lucky Red (Italy), Vértigo Films (Spain), Crunchyroll Store Australia (Australia and New Zealand) and Encore Films in Singapore and Southeast Asia.\\nNotably, The Secret World of Arrietty received a second dub exclusive to the United Kingdom, produced by StudioCanal UK, likely due to the film's origins being from Mary Norton's British novel The Borrowers.\\nDisney formerly held international sales rights until they were sold off to Goodfellas (then Wild Bunch) in 2003.\",\n",
       " \"He also announced that Toshio Suzuki would be replacing Kiyofumi Nakajima as president of Studio Ghibli, assuming the role for the first time since 2008, while Nakajima would continue to serve as a director. This change of management came about amidst reports that Suzuki had allegedly been mismanaging company funds by directing them towards his girlfriend's failed business ventures. This reportedly created tension between Suzuki and Hoshino, with the latter reportedly citing it as a long-term internal problem at the company since the couple met in 2013 and was the reason for his planned departure from the company, although a spokesperson for Studio Ghibli in a statement to Variety denied that Hoshino's departure had anything to do with these reports. The source of the allegations came from the tabloid paper, Shūkan Josei and was not corroborated by the mainstream media in Japan.\",\n",
       " \"Horus and Cagliostro were the feature-length directorial debuts of Isao Takahata and Hayao Miyazaki respectively, and were produced by Toei Animation and TMS Entertainment years before the founding of Studio Ghibli.\\nNausicaä was directed by Miyazaki at Topcraft, a studio which Miyazaki, Takahata and Toshio Suzuki later purchased and renamed Studio Ghibli. As a result, the film has often been rereleased and marketed as a Studio Ghibli movie.\\nThe Red Turtle was a collaborative effort by Studio Ghibli with Dutch animator Michaël Dudok de Wit and was branded as a Studio Ghibli release internationally. It was distributed by Sony Pictures Classics in North and Latin America.\\nFor the purposes of the list below, films that appear in Studio Ghibli's official filmography are listed. Other Studio Ghibli productions are listed here.\\n\\n\\n=== Feature films ===\"]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"What's Gihbli's famous policy?\"\n",
    "raw_results = existing_pipeline.query(query, k=20)\n",
    "raw_results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Oh! We can see in the results that the policy we're looking for is explained very clearly:\n",
    "\n",
    ">   'The studio is also known for its strict \"no-edits\" policy in licensing their films abroad due to Nausicaä of the Valley of the Wind being heavily edited for the film\\'s release in the United States as Warriors of the Wind.\\n\\n\\n=== Independent era ===\\nBetween 1999 and 2005, Studio Ghibli was a subsidiary brand of Tokuma Shoten; however, that partnership ended in April 2005, when Studio Ghibli was spun off from Tokuma Shoten and was re-established as an independent company with relocated headquarters.\\nOn February 1, 2008, Toshio Suzuki stepped down from the position of Studio Ghibli president, which he had held since 2005, and Koji Hoshino (former president of Walt Disney Japan) took over. Suzuki said he wanted to improve films with his own hands as a producer, rather than demanding this from his employees.',\n",
    "\n",
    "The problem is that it's ranked as the **14th** most relevant result! In a real RAG pipeline, this'd often be well outside the context you'd give to your LLM.\n",
    "\n",
    "This is where ColBERT re-ranking comes into play. Let's use our previously loaded `RAGPretrainedModel` to re-rank the results of our existing pipeline:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/bclavie/miniforge3/envs/ragatouille/lib/python3.11/site-packages/torch/amp/autocast_mode.py:250: UserWarning: User provided device_type of 'cuda', but CUDA is not available. Disabling\n",
      "  warnings.warn(\n",
      "100%|██████████| 1/1 [00:01<00:00,  1.97s/it]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'content': 'The studio is also known for its strict \"no-edits\" policy in licensing their films abroad due to Nausicaä of the Valley of the Wind being heavily edited for the film\\'s release in the United States as Warriors of the Wind.\\n\\n\\n=== Independent era ===\\nBetween 1999 and 2005, Studio Ghibli was a subsidiary brand of Tokuma Shoten; however, that partnership ended in April 2005, when Studio Ghibli was spun off from Tokuma Shoten and was re-established as an independent company with relocated headquarters.\\nOn February 1, 2008, Toshio Suzuki stepped down from the position of Studio Ghibli president, which he had held since 2005, and Koji Hoshino (former president of Walt Disney Japan) took over. Suzuki said he wanted to improve films with his own hands as a producer, rather than demanding this from his employees.',\n",
       "  'score': 15.333166122436523,\n",
       "  'rank': 0,\n",
       "  'result_index': 11},\n",
       " {'content': \"Studio Ghibli, Inc. (Japanese: 株式会社スタジオジブリ, Hepburn: Kabushiki-gaisha Sutajio Jiburi) is a Japanese animation studio based in Koganei, Tokyo. It has a strong presence in the animation industry and has expanded its portfolio to include various media formats, such as short subjects, television commercials, and two television films. Their work has been well-received by audiences and recognized with numerous awards. Their mascot and most recognizable symbol, the character Totoro from the 1988 film My Neighbor Totoro, is a giant spirit inspired by raccoon dogs (tanuki) and cats (neko). Among the studio's highest-grossing films are Spirited Away (2001), Howl's Moving Castle (2004), and Ponyo (2008).\",\n",
       "  'score': 12.45059871673584,\n",
       "  'rank': 1,\n",
       "  'result_index': 7},\n",
       " {'content': 'In September 2023, Nippon TV announced that Studio Ghibli would become a unit of Nippon Television Holdings in October.\\n\\n\\n== Name ==\\nThe name \"Ghibli\" was chosen by Miyazaki from the Italian noun ghibli (also used in English), an Italianization of the Libyan Arabic name for a hot desert wind (قبلي ghiblī) and nickname of the Italian Caproni Ca.309 aircraft. The name was chosen by Miyazaki due to his passion for aircraft and also for the idea that the studio would \"blow a new wind through the anime industry\". Although the Italian word would be more accurately transliterated as \"Giburi\" (ギブリ), with a hard g sound, the studio\\'s name is written in Japanese as Jiburi (ジブリ, [d͡ʑiꜜbɯ̟ᵝɾʲi] ).',\n",
       "  'score': 12.097004890441895,\n",
       "  'rank': 2,\n",
       "  'result_index': 1},\n",
       " {'content': \"Another defining feature is Hisaishi's unique use of leitmotif, rather than a singular song being associated with one character, the motif is the theme of the film. Hisaishi began using leitmotif in Ghibli films first in Howl's Moving Castle.\\n\\n\\n== Criticism ==\\nRayna Denison argues that, despite the feminist themes of Ghibli films, the studio has been reluctant to promote women within the company and regularly overworks its laborers, including on public holidays.Nathalie Pascaru and Maxim Tvorun-Dunn criticize the studio for what they see as the increasing commercialization of their films' iconography through plastic merchandise and tourist destinations, undermining the environmentalist themes of their films through industrial actions which are detrimental to the environment; as well as for turning their films' characters into decontextualized pop-culture references rather than multidimensional characters used to convey stories.\",\n",
       "  'score': 10.937824249267578,\n",
       "  'rank': 3,\n",
       "  'result_index': 0},\n",
       " {'content': \"It was a critical and commercial blockbuster, becoming the highest-grossing film in Japan of 1997, and also held Japan's box office record for domestic films until 2001's Spirited Away, another Miyazaki film. It was dubbed into English with a script by Neil Gaiman and initially distributed in North America by Miramax, where it sold well on home media despite not performing strongly at the box office. The film greatly increased Ghibli's popularity and influence outside Japan.\\n\\n\\n== Plot ==\\nIn Muromachi Japan, an Emishi village is attacked by a hideous demon. The last Emishi prince, Ashitaka, kills it before it reaches the village, but it grasps his arm and curses him before its death. The curse grants him superhuman strength, but it also causes him pain and will eventually kill him.\",\n",
       "  'score': 10.308815956115723,\n",
       "  'rank': 4,\n",
       "  'result_index': 15}]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "RAG.rerank(query=query, documents=raw_results, k=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And here it is! The relevant extract is now all the way at the top of the results, ready to be passed to the rest of your pipeline!\n",
    "\n",
    "So why not just use rerank() on the whole index if it's so good? Well, you could, but it's not very efficient. ColBERT is an extremely fast querier, but it needs to have an index built to do so. When you're using ColBERT to rerank documents, it's doing it index-free, which means it needs to encode all your documents and queries, and perform the comparison on the fly. This is fine for a handful of document on CPU or a few hundreds on GPU, but it's going to take exponentially longer as you add more documents!\n",
    "\n",
    "Re-ranking the results of another retrieval method is a good compromise: it allows you to leverage ColBERT's power without having to modify the rest of your pipeline, just increase the `k` value of your retriever and let ColBERT rescore them!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ragatouille",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
